import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Whip here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Whip extends Actor
{
    private GreenfootImage[][] skins=new GreenfootImage[20][2];
    private Player player;
    
    public Whip(Player character)
    {
        player=character;
        loadImages();
        
        setImage(new GreenfootImage(10,10));
    }
    
    public void loadImages()
    {
        for(int i=0;i<=7;i++)
        {
            skins[i][0]=new GreenfootImage("whipW"+i+".png");
            skins[i][1]=new GreenfootImage("whipW"+i+".png");
            skins[i][1].mirrorHorizontally();
        }
    }
    
    public void setSkin(int whipIndex,int dir)
    {
        setImage(skins[whipIndex][dir]);
        
        attach(whipIndex,dir);
    }
    
    public void hide()
    {
        GreenfootImage image=new GreenfootImage(10,10);
        image.clear();
        setImage(image);
        setLocation(360,400);
    }
    
    public void attach(int whipIndex,int dir)
    {
        int x=player.getX();
        int y=player.getY();
        int direction;
        
        if(dir==0)
        direction=1;
        else
        direction=-1;
        
        switch(whipIndex)
        {
            case 0:     setLocation(x+direction*(-60),y+(36));
                        break;
            case 1:     setLocation(x+direction*(-80),y+(20));
                        break;
            case 2:     setLocation(x+direction*(-57),y+(0));
                        break;
            case 3:     setLocation(x+direction*(65),y+(-30));
                        break;    
            case 4:     setLocation(x+direction*(102),y+(15));
                        break;
            case 5:     setLocation(x+direction*(-53),y+(35));
                        break;
            case 6:     setLocation(x+direction*(-64),y+(-44));
                        break;    
            case 7:     setLocation(x+direction*(80),y+(-123));
                        break;            
            default:    break;
        }
        
    }
    
    public void act() 
    {
        // Add your action code here.
    }    
    
    public boolean isTouchingLedge()
    {
        java.util.List<Ledge> ledge=getIntersectingObjects(Ledge.class);
        
        //Ledge ledge=(Ledge)getOneIntersectingObject(Ledge.class);
        
        if(ledge==null)
        return(false);
        
        //return(isVisuallyIntersecting(ledge));
        
        for(int i=0;i<ledge.size();i++)
        {
            if(isVisuallyIntersecting(ledge.get(i)))
            return(true);
        }
        return(false);
        //return(true);
    }
    
    private boolean isVisuallyIntersecting(Actor actor)    
    {    
        // get width and height of images    
        int wA = getImage().getWidth();    
        int hA = getImage().getHeight();    
        int wB = actor.getImage().getWidth();    
        int hB = actor.getImage().getHeight();    
        // get world coordinates of origin point (0, 0) of images    
        int xAw = getX()-wA/2;    
        int yAw = getY()-hA/2;    
        int xBw = actor.getX()-wB/2;    
        int yBw = actor.getY()-hB/2;    
        // determine (x, y) on each image to begin checking from    
        // and dimensions of area to check    
        int xA = 0, yA = 0, xB = 0, yB = 0; // initialize start coord fields    
        int w = 0, h = 0; // initialize scan dimension fields    
        // determine values for fields just initialized    
        if (xAw > xBw)    
        {    
            xB = xAw-xBw;    
            if (xAw+wA > xBw+wB) w = wB-xB; else w = wA;    
        }    
        else    
        {    
            xA = xBw-xAw;    
            if (xBw+wB > xAw+wA) w = wA-xA; else w = wB;    
        }    
        if (yAw > yBw)    
        {    
            yB = yAw-yBw;    
            if (yAw+hA > yBw+hB) h = hB-yB; else h = hA;    
        }    
        else    
        {    
            yA = yBw-yAw;    
            if (yBw+hB > yAw+hA) h = hA-yA; else h = hB;    
        }    
        // perform checking    
        java.awt.Color trans = new java.awt.Color(0, 0, 0, 0);    
        for (int x=0; x<w; x++) for (int y=0; y<h; y++)    
            {    
                if (!trans.equals(getImage().getColorAt(xA+x, yA+y)) &&    
                    !trans.equals(actor.getImage().getColorAt(xB+x, yB+y)))   
                    return true;  
        }    
        return false;    
    } 
}
